<?php
// +-----------------------------------------------------+
// | hyj
// |-----------------------------------------------------+
// | 文章管理模型
// |-----------------------------------------------------+
// | 2018-06-06 10:22
// +-----------------------------------------------------+
namespace app\admin\model;

use think\Model;

class ClassModel extends Model
{

    // 主键
    protected $pk = 'class_id';
    
    // 表名
    protected $table = 'tp_class';

    /**
     * 获取单个字段
     * @param  [type] $where [查询条件]
     * @param  [type] $field [查询字段]
     * @return [type]        [返回]
     */
    public function get_field($where, $field)
    {
        $data = $this->field($field)->where($where)->find();

        return $data->getAttr($field);
    }

    /**
     * 获取单条信息
     * @param  [type] $where [查询条件]
     * @param  [type] $field [查询字段]
     * @return [type]        [返回]
     */
    public function get_info($where, $fields = "*")
    {
        $data = $this->field($fields)->where($where)->find();

        return $data;

    }

    /**
     * 获取多条信息
     * @param  [type] $where [查询条件]
     * @param  [type] $order [排序规则]
     * @param  [type] $field [查询字段]
     * @return [type]        [返回]
     */
    public function get_list($where, $order = "", $fields = "*")
    {
        $order = empty($order) ? $this->pk." asc" : $order;
        $data  = $this->field($fields)->where($where)->order($order)->select();

        return $data->toArray();
    }    

    /**
     * 获取多条信息
     * @param  [type] $where [查询条件]
     * @param  [type] $order [排序规则]
     * @param  [type] $field [查询字段]
     * @param  [type] $start [跳过数量]
     * @param  [type] $size  [每页数量]
     * @return [type]        [返回]
     */

    public function get_list_page($where, $order = "", $fields = "*", $start, $size)
    {
        $order = empty($order) ? $this->pk." asc" : $order;
        $data  = $this->field($fields)->where($where)->order($order)->limit($start, $size)->select();

        return $data->toArray();
    }
    
    /**
     * 获取数量
     * @param $where
     */
    public function get_count($where)
    {
        return $this->where($where)->count();
    }

    /**
     * 插入单条信息
     * @param $param
     */
    public function insert_data($param)
    {
        try{

            $result = $this->create($param, true);

            return (int)$result->{$this->pk};

        }catch(\Exception $e){

            return msg_error($e->getMessage());
        }
    }

    /**
     * 批量编辑信息[插入与修改]
     * @param $param
     */
    public function edit_data_bantch($param)
    {
        try{

            return $this->saveAll($param);

        }catch(\Exception $e){

            return msg_error($e->getMessage());
        }
    }

    /**
     * 更新单条信息
     * @param $param
     */
    public function save_data($param)
    {
        try{

           return $this->update($param);

        }catch(PDOException $e){

            return msg_error($e->getMessage());
        }
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del_data($where)
    {
        try{

            return $this->where($where)->delete();

        }catch(PDOException $e){

            return msg_error($e->getMessage());
        }
    }

    public function news()
    {
        return $this->belongsTo('NewsModel');
        
    }
}
